﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using System.Web.Routing;
using CMN.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using CMN.Controllers.Filters;
namespace CMN.Controllers
{
    [ExceptionFilter]
    [LoggerFilter]
    [UserAuthorize]
    public class Normal_ZTController : Controller
    {
        
        // 返回json字符串
        protected string returnJson = string.Empty;

        /// <summary>
        /// 转到View页面
        /// </summary>
        /// <returns></returns>
        public ViewResult Index()
        {
            return View("Index");//页面地址
        }

        /// <summary>
        /// 返回专题列表
        /// </summary>
        /// <returns>json</returns>
        public string List()
        {
            //返回json数组
          
            int pageSize = (int)Utils.GetQueryAndFormString("limit", this, 4, 1, DataType.Int);
            int pageIndex = (int)Utils.GetQueryAndFormString("start", this, 9, 1, DataType.Int);
            QueryParam qp = new QueryParam();
            qp.PageIndex = pageIndex / pageSize + 1; ;
            qp.PageSize = pageSize;
            int recordNum = 0;
            IsoDateTimeConverter jsDataConvert= new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            jsDataConvert.DateTimeFormat = "yyyy-MM-dd";
            IDataService<Normal_ZTEntity> Normal_ZTSv = ServiceBuilder.BuildNormal_ZTService();
            List<Normal_ZTEntity> normal_ztList = Normal_ZTSv.List(qp, out recordNum);
            returnJson = string.Concat("{totalPorperty:", recordNum, ",root:", JsonConvert.SerializeObject(normal_ztList, Formatting.Indented, jsDataConvert), "}");
            return returnJson;
 
        }

        /// <summary>
        /// 查看专题 
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Look(string actionId)
        {
           IDataService<Normal_ZTEntity> Normal_ZTSv = ServiceBuilder.BuildNormal_ZTService();
            int intId=Utils.StringToInt(actionId,0);
            Normal_ZTEntity normal_zt = Normal_ZTSv.Look(intId);
            IsoDateTimeConverter jsDataConvert = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            jsDataConvert.DateTimeFormat = "yyyy-MM-dd";
            if (normal_zt != null)
                returnJson = string.Concat("{success:true,data:", JsonConvert.SerializeObject(normal_zt, Formatting.Indented, jsDataConvert), "}");
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 更新专题
        /// </summary>
        /// <param name="actionId"></param>
        /// <returns>json</returns>
        public string Update(string actionId)
        {
            int intId = Utils.StringToInt(actionId, 0);
            if (intId != 0)
            {
                Normal_ZTEntity normal_zt = new Normal_ZTEntity();
                normal_zt.DataTable_Action = DataTableAction.Update;
                normal_zt.Id = intId;
                normal_zt.Title = (String)Utils.GetQueryAndFormString("Title", this, 50,  1 , DataType.Str);
                normal_zt.SEOTitle = (String)Utils.GetQueryAndFormString("SEOTitle", this, 100,  0, DataType.Str);
                normal_zt.Summary = (String)Utils.GetQueryAndFormString("Summary", this, 480,  0, DataType.Str);
                normal_zt.Dir = (String)Utils.GetQueryAndFormString("Dir", this, 50,  1 , DataType.Str);
                normal_zt.TopImg = (String)Utils.GetQueryAndFormString("TopImg", this, 300,  0, DataType.Str);
                normal_zt.Addate = (DateTime)Utils.GetQueryAndFormString("Addate", this, 23,  1 , DataType.Dat);
                normal_zt.OrderNum = (Int32)Utils.GetQueryAndFormString("OrderNum", this, 10,  0, DataType.Int);
                normal_zt.ChannelPath = (String)Utils.GetQueryAndFormString("ChannelPath", this, 20,  0, DataType.Str);
                normal_zt.Enabled = (Int32)Utils.GetQueryAndFormString("Enabled", this, 3, 1, DataType.Int);
                normal_zt.ClickNum = (Int32)Utils.GetQueryAndFormString("ClickNum", this, 10,  0, DataType.Int);
                normal_zt.KeyWords = (String)Utils.GetQueryAndFormString("KeyWords", this, 60, 0, DataType.Str);
                normal_zt.TemplateId = (Int32)Utils.GetQueryAndFormString("TemplateId", this, 10, 1, DataType.Int);
                IDataService<Normal_ZTEntity> Normal_ZTSv = ServiceBuilder.BuildNormal_ZTService();
                if (Normal_ZTSv.Update(normal_zt)== "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }
           
            return returnJson;
        }

        /// <summary>
        /// 新增专题
        /// </summary>
        /// <returns>json</returns>
        public string Insert()
        {
            Normal_ZTEntity normal_zt = new Normal_ZTEntity();
            normal_zt.DataTable_Action = DataTableAction.Insert;
            normal_zt.Title = (String)Utils.GetQueryAndFormString("Title", this, 50,  1 , DataType.Str);
            normal_zt.SEOTitle = (String)Utils.GetQueryAndFormString("SEOTitle", this, 100,  0, DataType.Str);
            normal_zt.Summary = (String)Utils.GetQueryAndFormString("Summary", this, 480,  0, DataType.Str);
            normal_zt.Dir = (String)Utils.GetQueryAndFormString("Dir", this, 50,  0, DataType.Str);
            normal_zt.TopImg = (String)Utils.GetQueryAndFormString("TopImg", this, 300,  0, DataType.Str);
            normal_zt.Addate = (DateTime)Utils.GetQueryAndFormString("Addate", this, 23,  1 , DataType.Dat);
            normal_zt.OrderNum = (Int32)Utils.GetQueryAndFormString("OrderNum", this, 10,  0, DataType.Int);
            normal_zt.ChannelPath = (String)Utils.GetQueryAndFormString("ChannelPath", this, 20,  0, DataType.Str);
            normal_zt.Enabled = (Int32)Utils.GetQueryAndFormString("Enabled", this, 3, 0, DataType.Int);
            normal_zt.ClickNum = (Int32)Utils.GetQueryAndFormString("ClickNum", this, 10,  0, DataType.Int);
            normal_zt.KeyWords = (String)Utils.GetQueryAndFormString("KeyWords", this, 60, 0, DataType.Str);
            normal_zt.TemplateId = (Int32)Utils.GetQueryAndFormString("TemplateId", this, 10, 1, DataType.Int);
            if (string.IsNullOrEmpty(normal_zt.Dir))
            {
                normal_zt.Dir = Utils.ChineseToQuanPin(normal_zt.Title);
            }
            int recordNum = 0;
           
           IDataService<Normal_ZTEntity> Normal_ZTSv = ServiceBuilder.BuildNormal_ZTService();
           QueryParam qp2 = new QueryParam();
           qp2.PageIndex = 1;
           qp2.PageSize = 1;
           qp2.Where = string.Concat("dir='", normal_zt.Dir, "'");
           List<Normal_ZTEntity> channnelList2 = Normal_ZTSv.List(qp2, out recordNum);
           if (channnelList2.Count > 0)
           {
               return "{success:'false',message:'重复的专题目录'}";
           }
            if (Normal_ZTSv.Insert(normal_zt)== "")
            {
                returnJson = "{success:'true'}";
            }
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 删除专题
        /// </summary>
        /// <param name="actionId"></param>
        /// <returns>json</returns>
        public string Delete(string actionId)
        {
            int intId = Utils.StringToInt(actionId, 0);
            if (intId != 0)
            {
                Normal_ZTEntity normal_zt = new Normal_ZTEntity();
                normal_zt.Id = intId;
                IDataService<Normal_ZTEntity> Normal_ZTSv = ServiceBuilder.BuildNormal_ZTService();
                IDataService<Normal_ZTMapEntity> Normal_ZTMapSv = ServiceBuilder.BuildNormal_ZTMapService();
                QueryParam qp = new QueryParam();
                qp.PageSize = 1;
                qp.Where = string.Concat(" pid=", intId.ToString());
                int recourdNum = 0;
                if (Normal_ZTMapSv.List(qp, out recourdNum).Count > 0)
                {
                    returnJson = "{success:'false',message:'请先删除子类别'}";
                }
                else
                {
                    if (Normal_ZTSv.Delete(normal_zt) == "")
                    {
                        returnJson = "{success:'true'}";
                    }
                    else
                    {
                        returnJson = "{success:'false'}";
                    }
                }
            }


            else
            {
                returnJson = "{success:'false'}";
            }
            return returnJson;
        }

        /// <summary>
        /// 批量删除专题
        /// </summary>
        /// <param name="idList">id列表 234_4_5</param>
        /// <returns>json</returns>
        public string DeleteList(string actionId)
        {
            //Common.RegExpression.NumAndBarRegExpression regex = new CMN.Common.RegExpression.NumAndBarRegExpression();
            //if (regex.IsMatch(actionId))
            //{

            //    IDataService<Normal_ZTEntity> Normal_ZTSv = ServiceBuilder.BuildNormal_ZTService();
            //    if (Normal_ZTSv.DeleteList(actionId)== "")
            //    {
            //        returnJson = "{success:'true'}";
            //    }
            //    else
            //        returnJson = "{success:'false'}";

            //}
            //else
            //{
            //    returnJson = "{success:'false',message:'参数类型不正确'}";
            //}
            //return returnJson;
            return "";
        }

    }
}
